package com.ykl.cas.config;

import org.apereo.cas.authentication.AuthenticationEventExecutionPlan;
import org.apereo.cas.authentication.AuthenticationEventExecutionPlanConfigurer;
import org.apereo.cas.authentication.AuthenticationHandler;
import org.apereo.cas.authentication.principal.DefaultPrincipalFactory;
import org.apereo.cas.configuration.CasConfigurationProperties;
import org.apereo.cas.services.ServicesManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration("customAuthenticationConfiguration")
@EnableConfigurationProperties(CasConfigurationProperties.class)
public class CustomAuthenticationConfiguration implements AuthenticationEventExecutionPlanConfigurer {

  @Autowired
  private CasConfigurationProperties casProperties;

  @Autowired
  @Qualifier("servicesManager")
  private ServicesManager servicesManager;


  @Bean
  public AuthenticationHandler myAuthenticationHandler() {
    // 参数: name, servicesManager, principalFactory, order
    // 定义为优先使用它进行认证
    return new com.ykl.cas.authentication.CustomerHandlerAuthentication(com.ykl.cas.authentication.CustomerHandlerAuthentication.class.getName(),
            servicesManager, new DefaultPrincipalFactory(), 1);
  }

  @Override
  public void configureAuthenticationExecutionPlan(final AuthenticationEventExecutionPlan plan) {
    plan.registerAuthenticationHandler(myAuthenticationHandler());
  }
}